<template>
  <div class="products">
    <div class="products-header">
      <h1>产品展示</h1>
      <p>浏览我们的产品系列</p>
    </div>
    
    <div class="search-bar">
      <input 
        type="text" 
        v-model="searchQuery" 
        placeholder="搜索产品..."
        class="search-input"
      >
    </div>
    
    <div class="products-grid">
      <div 
        v-for="product in filteredProducts" 
        :key="product.id"
        class="product-card"
        @click="goToProduct(product.id)"
      >
        <div class="product-image">
          <img :src="product.image" :alt="product.name">
        </div>
        <div class="product-info">
          <h3>{{ product.name }}</h3>
          <p class="product-description">{{ product.description }}</p>
          <div class="product-price">¥{{ product.price }}</div>
          <div class="product-category">{{ product.category }}</div>
        </div>
      </div>
    </div>
    
    <div v-if="filteredProducts.length === 0" class="no-products">
      <p>没有找到匹配的产品</p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Products',
  data() {
    return {
      searchQuery: '',
      products: [
        {
          id: 1,
          name: 'Vue.js 开发指南',
          description: '全面学习Vue.js框架的完整教程',
          price: 89,
          category: '教程',
          image: 'https://via.placeholder.com/300x200/3498db/ffffff?text=Vue.js+Guide'
        },
        {
          id: 2,
          name: 'JavaScript 高级编程',
          description: '深入理解JavaScript语言特性',
          price: 128,
          category: '编程',
          image: 'https://via.placeholder.com/300x200/e74c3c/ffffff?text=JavaScript+Pro'
        },
        {
          id: 3,
          name: 'Web 前端开发工具',
          description: '提高开发效率的实用工具集',
          price: 199,
          category: '工具',
          image: 'https://via.placeholder.com/300x200/2ecc71/ffffff?text=Dev+Tools'
        },
        {
          id: 4,
          name: 'React 实战项目',
          description: '通过实际项目学习React开发',
          price: 156,
          category: '教程',
          image: 'https://via.placeholder.com/300x200/9b59b6/ffffff?text=React+Project'
        },
        {
          id: 5,
          name: 'Node.js 后端开发',
          description: '构建高性能的Node.js应用',
          price: 178,
          category: '后端',
          image: 'https://via.placeholder.com/300x200/f39c12/ffffff?text=Node.js+Backend'
        },
        {
          id: 6,
          name: 'CSS 动画设计',
          description: '创建炫酷的CSS动画效果',
          price: 99,
          category: '设计',
          image: 'https://via.placeholder.com/300x200/1abc9c/ffffff?text=CSS+Animation'
        }
      ]
    }
  },
  computed: {
    filteredProducts() {
      if (!this.searchQuery) {
        return this.products
      }
      return this.products.filter(product => 
        product.name.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
        product.description.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
        product.category.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  },
  methods: {
    goToProduct(id) {
      this.$router.push(`/products/${id}`)
    }
  }
}
</script>

<style scoped>
.products {
  max-width: 1200px;
  margin: 0 auto;
}

.products-header {
  text-align: center;
  margin-bottom: 3rem;
}

.products-header h1 {
  color: #2c3e50;
  font-size: 2.5rem;
  margin-bottom: 1rem;
}

.products-header p {
  color: #7f8c8d;
  font-size: 1.2rem;
}

.search-bar {
  margin-bottom: 2rem;
  text-align: center;
}

.search-input {
  width: 100%;
  max-width: 400px;
  padding: 1rem;
  border: 1px solid #ddd;
  border-radius: 25px;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.3s;
}

.search-input:focus {
  border-color: #3498db;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
  margin-bottom: 2rem;
}

.product-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 15px rgba(0,0,0,0.2);
}

.product-image {
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  padding: 1.5rem;
}

.product-info h3 {
  color: #2c3e50;
  margin-bottom: 0.5rem;
  font-size: 1.2rem;
}

.product-description {
  color: #7f8c8d;
  margin-bottom: 1rem;
  line-height: 1.5;
}

.product-price {
  font-size: 1.5rem;
  font-weight: bold;
  color: #e74c3c;
  margin-bottom: 0.5rem;
}

.product-category {
  background: #ecf0f1;
  color: #2c3e50;
  padding: 0.3rem 0.8rem;
  border-radius: 15px;
  font-size: 0.9rem;
  display: inline-block;
}

.no-products {
  text-align: center;
  padding: 3rem;
  color: #7f8c8d;
  font-size: 1.2rem;
}
</style>
